<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="displayModuleTitle"
    @ok="onOkButtonClick"
  >
    <BasicForm @register="registerForm"></BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getOrgDeptList } from '/@/api/ehs/organization';
  import { queryProjectUsersDetail } from '/@/api/project/user';
  import { transmitItem } from '/@/api/ehs/taskItem';
  import { transmitFormSchema } from './taskBench.data';
  import { useGlobSetting } from '/@/hooks/setting';
  const { globalProjectCode } = useGlobSetting();
  const { createMessage } = useMessage();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    name: 'TransmitTaskItemModal',
    emits: ['success'],
    props: ['orgcode'],
    setup(props, { emit }) {
      const [registerForm, { validate, updateSchema, resetFields }] = useForm({
        labelWidth: 100,
        schemas: transmitFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const itemid = ref();
      const deptData = ref([]);
      const userData = ref([]);
      const [register, { closeModal }] = useModalInner((data) => {
        if (data != undefined && data != null) {
          resetFields();
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.dataId) {
            itemid.value = unrefData.dataId;
          }
          getOrgDeptList(props.orgcode).then((res1) => {
            deptData.value = res1;
            queryProjectUsersDetail({ project: globalProjectCode }).then((res2) => {
              userData.value = res2;
              updateSchema([
                {
                  field: 'transdeptid',
                  componentProps: {
                    options: res1,
                    listHeight: 220,
                    getPopupContainer: (triggerNode) => document.body,
                    fieldNames: { label: 'name', value: 'id' },
                  },
                },
                {
                  field: 'transuserid',
                  componentProps: {
                    options: res2,
                    listHeight: 220,
                    getPopupContainer: (triggerNode) => document.body,
                    fieldNames: { label: 'name', value: 'id' },
                  },
                },
              ]);
            });
          });
        }
      });
      async function onOkButtonClick() {
        const values = await validate();
        let params = {
          transdeptid: values.transdeptid,
          transdeptname: deptData.value.find((item) => item.id == values.transdeptid)?.name,
          transuserid: values.transuserid,
          transusername: userData.value.find((item) => item.id == values.transuserid)?.name,
        };
        transmitItem({ ...params, itemid: itemid.value }).then((res) => {
          if (!res) {
            createMessage.success('任务项转交成功');
            emit('success');
            closeModal();
            resetFields();
          } else {
            createMessage.error('任务项转交失败');
          }
        });
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      return {
        register,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
      };
    },
  });
</script>
